unit picker;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, dbbase, StdCtrls, Buttons, Mask, RzEdit, RzRadChk, RzButton,
  Grids, DBGrids, RzDBGrid, ExtCtrls, DB, StrUtils;

type
  Tpickerfrm = class(TdbbaseFrm)
  private
    { Private declarations }
  public
    { Public declarations }
    Procedure IniParam;override;
    procedure QFindRcd;override;
    procedure PrintRpt;override;
    procedure FindRcd;override;                  
    procedure AddRcd;override;
    procedure EditRcd;override;
    procedure DelRcd;override;
    procedure CutCnnt;override;
    procedure FrmExit;override;
  end;

var
  pickerfrm: Tpickerfrm;

implementation

{$R *.dfm}

uses includefile, dbmodule, editpicker;

procedure Tpickerfrm.IniParam;
var
  i:integer;
begin 
  for i:=0 to 12 do begin
    dbgrid.Columns[i].Title.Caption:=GetCaption('pickerfrm',5001+i);
  end;
  DataSource.DataSet:=dbmodulefrm.dbquery;
  with dbmodulefrm.dbquery  do begin
    active:=False;
    sql.Clear;
    sql.Add(FORMAT('Select Right(RFID,%d) As RFID, [NAME], SEX, Format(BIRTHDAY,"%s") as BIRTHDAY, ',[RFIDLEN,DateFrtStr]));
    sql.Add('TFN, TFNTYPE, CONTRACTORNAME, ID_DOC, MOB_TEL, ADDRESS, EMAIL, EXT, HM_TEL ');
    sql.add('From employee');
    //showmessage(sql.Text);
    execsql;
    active:=True;
    for i:=0 to 12 do begin
      dbgrid.Columns[i].FieldName:=Fields[i].FieldName;
    end;
  end;
end;

procedure Tpickerfrm.QFindRcd;
var
  tmp:string;
begin
  tmp:=Trim(edit_qsearch.Text);
  with DBGrid.DataSource.DataSet do begin
    Active:=False;
    Filter:=Format('RFID like ''*%s*'' or [NAME] like ''*%s*'''+
                   ' or SEX like ''*%s*'' or TFN like ''*%s*'''+
                   ' or ID_DOC like ''*%s*'' or MOB_TEL like ''*%s*''' +
                   ' or ADDRESS like ''*%s*'' or EXT like ''*%s*'''+
                   ' or HM_TEL like ''*%s*'' or BIRTHDAY like ''*%s*''',
                   [tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp,tmp]);
    Filtered:=True;
    Active:=True;
  end;
end;

procedure Tpickerfrm.PrintRpt;
begin

end;

procedure Tpickerfrm.FindRcd;
begin

end;

procedure Tpickerfrm.AddRcd;
begin
  with Teditpickerfrm.Create(Application) do begin
    Caption:=GetCaption('editpickerfrm',1000);
    if mrok=showmodal then begin
      dbmodulefrm.dbquery.Active:=False;
      dbmodulefrm.dbquery.SQL.Clear;
      dbmodulefrm.dbquery.SQL.Add('INSERT INTO employee ');
      dbmodulefrm.dbquery.SQL.Add('(RFID,[NAME],SEX,BIRTHDAY,TFN,TFNTYPE,CONTRACTORNAME,ID_DOC,MOB_TEL,ADDRESS,EMAIL,EXT,HM_TEL) VALUES');
      dbmodulefrm.dbquery.SQL.Add(Format('(''%s'',''%s'',''%s'',#%s#,''%s'',%d,%d,''%s'',''%s'',''%s'',''%s'',''%s'',''%s'')',
                                        [RightStr('0000000000'+txtRFID.Text,10),txtNAME.text,cmbxSex.Text,FormatDateTime('yyyy-MM-dd',dtpBthday.Date),
                                         txtTFN.text,cmbxTfntype.itemindex,strtoint(Trim(LeftStr(cmbxcontractor.text,4))),txtLKman.text,
                                         txtLkmantel.text,txtaddress.text,txtemail.text,txtext.text,txthm_tel.text]));
      dbmodulefrm.dbquery.ExecSQL;
      iniParam;
    end;
  end;
end;

procedure Tpickerfrm.EditRcd;
var
  tmp:string;
  oldRFID:string;
begin
  with Teditpickerfrm.Create(Application) do begin
    Caption:=GetCaption('editpickerfrm',999);
    txtRFID.Text:=dbgrid.DataSource.DataSet.Fieldbyname('RFID').AsString;
    oldRFID:=txtRFID.Text;
    txtName.Text:=dbgrid.DataSource.DataSet.fieldbyname('NAME').AsString;
    tmp:=dbgrid.DataSource.DataSet.fieldbyname('NAME').AsString;
    if tmp='FEMALE' then
      cmbxSex.ItemIndex:=1
    else
      cmbxSex.ItemIndex:=0;
    txtLkMan.Text:=dbgrid.DataSource.DataSet.fieldbyname('ID_DOC').AsString;
    txtLKManTel.Text:=dbgrid.DataSource.DataSet.fieldbyname('MOB_TEL').AsString;
    txtAddress.Text:=dbgrid.DataSource.DataSet.fieldbyname('ADDRESS').AsString;
    txtTFN.Text:=dbgrid.DataSource.DataSet.fieldbyname('TFN').AsString;
    txtemail.Text:=dbgrid.DataSource.DataSet.fieldbyname('EMAIL').AsString;
    txtExt.Text:=dbgrid.DataSource.DataSet.fieldbyname('EXT').AsString;
    txtHM_Tel.Text:=dbgrid.DataSource.DataSet.fieldbyname('HM_TEL').AsString;
    case dbgrid.DataSource.DataSet.fieldbyname('TFNTYPE').AsInteger of
      0:cmbxTFNTYPE.ItemIndex:=0;
      1:cmbxTFNTYPE.ItemIndex:=1;
      else cmbxTFNTYPE.ItemIndex:=2;
    end;
    try
      dtpbthday.DateTime:=Str2Date(dbgrid.DataSource.DataSet.fieldbyname('BIRTHDAY').AsString);
    except
      dtpbthday.DateTime:=now;
    end;
    cmbxcontractor.Text:=dbmodulefrm.getConstractorNameByID(
                                dbgrid.DataSource.DataSet.fieldbyname('CONTRACTORNAME').asInteger);
    if mrok=showmodal then begin
      dbmodulefrm.dbquery.Active:=False;
      dbmodulefrm.dbquery.SQL.Clear;
      dbmodulefrm.dbquery.SQL.Add('Update employee ');
      dbmodulefrm.dbquery.sql.Add(Format('SET RFID=''%s'', [NAME]=''%s'', SEX=''%s'', BIRTHDAY=#%s#,',
                [RightStr('0000000000'+txtRFID.Text,10),txtNAME.text,cmbxSex.Text,FormatDateTime('yyyy-MM-dd',dtpBthday.Date)]));
      dbmodulefrm.dbquery.sql.Add(Format('TFN=''%s'', TFNTYPE=%d, CONTRACTORNAME=%d, ID_DOC=''%s'',',
                                          [txtTFN.text,cmbxTfntype.itemindex,strtoint(Trim(LeftStr(cmbxcontractor.text,4))),txtLKman.text]));
      dbmodulefrm.dbquery.sql.Add(Format('MOB_TEL=''%s'', ADDRESS=''%s'', EMAIL=''%s'',EXT=''%s'',HM_TEL=''%s''',
                                          [txtLkmantel.text,txtaddress.text,txtemail.text,txtext.text,txthm_tel.text]));
      dbmodulefrm.dbquery.sql.Add(Format('Where Right(RFID,%d)=''%s''',[RFIDLEN,oldRFID]));
      dbmodulefrm.dbquery.ExecSQL;
      iniParam;
    end;
  end;
end;

procedure Tpickerfrm.DelRcd;
var
  tmpRFID:string;
begin
  if IDYES=Application.MessageBox(pchar(GetCaption('Msgbox',103)),
                                  pchar(GetCaption('Msgbox',104)),MB_YESNO) then begin
    tmpRFID:=dbgrid.DataSource.DataSet['RFID'];
    with dbmodulefrm.dbquery  do begin
      active:=False;
      sql.Clear;
      sql.Add(Format('Delete from employee where Right(RFID,%d)=''%s''',[RFIDLEN,tmpRFID]));
      execsql;
    end;
    IniParam;
  end;
end;

procedure Tpickerfrm.CutCnnt;
begin

end;

procedure Tpickerfrm.FrmExit;
begin
  dbmodulefrm.dbquery.Active:=False;
  Hide;
end;

end.


